<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * create category editor page
 * get: category.browse.tpl.php
 * used from category.page.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
db::getInstance()->where("category_id", $refValidate->cleanslash($_GET['id']));
$result = db::getInstance()->getOne("category");

if ($result === NULL)
{ // if error...
	message::setErrorRow(_('Databaseerror in Table CATEGORY - Contact the Admin!'), true);
}
else
{
	$refPage->assign('page_title', _('Product Category Editor'));
	$refPage->assign('textHelp', help('categoryText', $page, TRUE));

	$refPage->assign('id', _('Category-ID') . ': ');
	$refPage->assign('idx',
		'<input type="text" name="idx" id="idx" disabled="disabled" ' .
		'size="8" value="' . $result['category_id'] . '"/>');

	$refPage->assign('name', _('Name') . ': ');
	$refPage->assign('namex', $result['category_name']);
	$refPage->assign('namexHelp', help('categoryName', $page));

	// set activ-status
	$refPage->assign('active', _('Activ') . ': ');
	if ($result['category_active'] === '0')
	{
		$refPage->assign('isActive', '');
	}
	else
	{
		$refPage->assign('isActive', 'checked');
	}

	$refPage->assign('descriptionx', $result['category_description']);

	// image
	$refPage->assign('image', _('Big image') . ': ');
	$refPage->assign('imagex', '<input type="file" size="60" name="uploadfile" id="uploadfile" />');
	$refPage->assign('imagexHelp', help('categoryBigImage', $page));

	$refPage->assign('imageName', $result['category_image']);
	$refPage->assign('imageNote', _('Maximum') . ': ' . CATEGORY_IMAGE_SIZE . ' ' .
		_('Bytes'));

	$file = '../images/' . CATEGORY_IMAGE_PATH . '/' . $result['category_id'] . '/' .
		$result['category_image'];
	if (is_file($file))
	{
		$refPage->assign('imagey',
			'<img style="border:1px solid black;" src="../images/' . CATEGORY_IMAGE_PATH . '/' .
			$result['category_id'] . '/' . $result['category_image'] . '">');
	}
	else
	{
		$refPage->assign('imagey', _('<h2>No picture available!</h2>'));
	}

	// small-image
	$refPage->assign('thumbImage', _('Small image') . ': ');
	$refPage->assign('thumbImagex',
		'<input type="file" class="ngggg" size="60" name="smallUploadfile" id="smallUploadfile" />');
	$refPage->assign('thumbImageyHelp', help('categoryThumbImage', $page));

	$refPage->assign('thumbImageName', $result['category_small_image']);
	$refPage->assign('thumbImageNote',
		_('Maximum') . ': ' . CATEGORY_THUMB_WITH . 'px ' .
		_('width') . ' * ' . CATEGORY_THUMB_HEIGHT . 'px ' .
		_('height'));

	$file = '../images/' . CATEGORY_IMAGE_PATH . '/' . $result['category_id'] . '/' .
		CATEGORY_IMAGE_THUMB_PATH . '/' . $result['category_small_image'];

	if (is_file($file))
	{
		$refPage->assign('thumbImagey',
			'<img style="border:1px solid black;" src="../images/' . CATEGORY_IMAGE_PATH . '/' .
			$result['category_id'] . '/' . CATEGORY_IMAGE_THUMB_PATH . '/' .
			$result['category_small_image'] . '">');
	}
	else
	{
		$refPage->assign('thumbImagey', _('<h2>No picture available!</h2>'));
	}

	// read and set article-browsepages  selected="selected"
	$browsepages = array();
	foreach (getBrowsepages('category') as $file)
	{
		if ($file === $result['category_browsepage'])
		{
			$browsepages[] = array('browsepage_nrx' => $file,
			 'browsepage_textx' => $file,
			 'checkedx' => 'selected="selected"');
		}
		else
		{
			$browsepages[] = array('browsepage_nrx' => $file,
			 'browsepage_textx' => $file,
			 'checkedx' => '');
		}
	}
	$refPage->assign('browsepages', $browsepages);
	$refPage->assign('browsepage', _('Browsepage') . ': ');
	$refPage->assign('browsepagexHelp', help('categoryBrowsepage', $page));

	// read and set categorynames
	$categorys = db::getInstance()->rawQuery("SELECT category_id, category_name, category_active, " .
		"category_order, category_top FROM category WHERE category_top = '0'" .
		" ORDER BY category_order");

	$categorys = sortCategorys($categorys, false);
	$categoryTops = array();
	if ($result['category_top'] === '0')
	{
		$categoryTops[] = array('top_nrx' => '0',
		 'top_textx' => _('Top-category'),
		 'checkedx' => 'selected="selected"');
	}
	else
	{
		$categoryTops[] = array('top_nrx' => '0',
		 'top_textx' => _('Top-category'),
		 'checkedx' => '');
	}
	foreach ($categorys as $cat)
	{
		if ($cat['category_id'] === $result['category_top'])
		{
			$categoryTops[] = array('top_nrx' => $cat['category_id'],
			 'top_textx' => $cat['category_name'],
			 'checkedx' => 'selected="selected"');
		}
		else
		{
			$categoryTops[] = array('top_nrx' => $cat['category_id'],
			 'top_textx' => $cat['category_name'],
			 'checkedx' => '');
		}
	}
	$refPage->assign('categoryTops', $categoryTops);
	$refPage->assign('categoryTop', _('Top-Category') . ': ');
	$refPage->assign('categoryTopxHelp', help('categoryCategory', $page));

	$refPage->assign('toshow', _('Display Products') . ': ');
	$refPage->assign('toshowx', $result['category_display_products']);
	$refPage->assign('toShowxHelp', help('categoryToShow', $page));

	$refPage->assign('metadescription', _('Metadescrip.') . ': ');
	$refPage->assign('metadescriptionNo',
		'onmouseover="descriptionN(this.value)" onkeyup="descriptionN(this.value)"');
	$refPage->assign('metadescriptionx', $result['category_metadescription']);
	$refPage->assign('metadescriptionxHelp', help('categoryMetadescription', $page));

	$refPage->assign('metakeyword', _('Meta-Keywords') . ': ');
	$refPage->assign('metakeywordNo',
		'onmouseover="keywordN(this.value)" onkeyup="keywordN(this.value)"');
	$refPage->assign('metakeywordx', $result['category_metakey']);
	$refPage->assign('metakeywordxHelp', help('categoryMetakeyword', $page));

	$refPage->assign('create', _('Created') . ': ');
	$refPage->assign('createx',
		'<input type="text" name="createx" id="createx" ' .
		'disabled="disabled" value="' .
		date('H:i:s - d.m.Y', strtotime($result['category_create'])) . '"/>');

	$refPage->assign('lastchange', _('Last change') . ': ');
	$refPage->assign('lastchangex',
		'<input type="text" name="lastchangex" id="lastchangex" ' .
		'disabled="disabled" value="' .
		date('H:i:s - d.m.Y', strtotime($result['category_last_change'])) . '"/>' .
		'<input type="hidden" name="idx" value="' . $result['category_id'] . '">' .
		'<input type="hidden" name="orderx" value="' . $result['category_order'] . '">' .
		'<input type="hidden" name="oldImagex" value="' . $result['category_image'] . '">' .
		'<input type="hidden" name="oldSmallImagex" value="' . $result['category_small_image'] . '">' .
		'<input type="hidden" name="oldtopx" value="' . $result['category_top'] . '">');

	$refPage->assign('btnRefresh',
		'<input type="submit" name="submit" id="refresh" value="' .
		_('Refresh') . '" />');
	$refPage->assign('btnSave',
		'<input type="submit" name="submit" id="save" value="' .
		_('Save') . '" />');
	$refPage->assign('btnAbort',
		'<input type="submit" name="submit" id="abort" value="' .
		_('Abort') . '">');

	$refPage->assign('Form_Start',
		'<form action="' . 'index2.php?page=article&sub=category&id=' .
		$result['category_id'] . '&func=save' .
		'" method="post" class="niceform fieldset" ' .
		'enctype="multipart/form-data"><fieldset>');
	$refPage->addtpl('content', '/' . $page . '/' . $subpage . '.editor.tpl.php');
	$refPage->assign('messagebox', '');
	$refPage->assign('Form_End', '</fieldset></form>');


	// Keyword- and descriptioncounter
	$refPage->assign('descriptionNumber', '<span id="descriptionNumber">0</span>');
	$refPage->assign('keywordNumber', '<span id="keywordNumber">0</span>');

	$metascript =
		'<script type="text/javascript">
		function descriptionN(text) 
		{
			var html = text.length;
			html = html + "   ' . _('Character') . ' (' . METADESCRIPTION_LENGH . ')";
			document.getElementById(\'descriptionNumber\').textContent = html;
		}
		function keywordN(text) 
		{
			var html = text.split(\',\').length;
			html = html + "   ' . _('Words') . ' (' . METAKEYWORD_NUMBER . ')";
			document.getElementById(\'keywordNumber\').textContent = html;
		}
	</script>';

	$refPage->assign('scripts',
		'<script type="text/javascript" src="' .
		'module/ckeditor/ckeditor.js"></script>' . $metascript);
}
?>